package visualization.renderers
{
	import model.Field;
	
	import visualization.IDataPoint;
	import visualization.Visualization;
	
	public class OneDimensional extends VisualizationRenderer
	{
		public var elementSize:Object = 5;
		public var xAxis:Field;
		public var xMax:Object = 0;
		public var xMin:Object = 0;
		public var lineColor:Object = 0;
		public var lineThickness:Object = 3;
		
		public var toolTip:Object = null;
		
		public function OneDimensional()
		{
			super();
		}
		
		override public function renderFull(source:Visualization):void{
			source.background.graphics.clear();
			
			// Draw axis line
			var lineColor:uint = source.getValue(this.lineColor) as Number;
			var lineHeight:Number = source.getValue(this.lineThickness) as Number;
			source.background.graphics.beginFill(lineColor);
			source.background.graphics.drawRect(0,(source.height - lineHeight)/2,source.width,lineHeight);
			
			// Plot points
			for each(var point:Object in this.getLayout(source)){
				var element:IDataPoint = point.item as IDataPoint;
				element.visible = true;
				element.x = point.x;
				element.y = point.y;
				element.width = point.width;
				element.height = point.height;
				
				// Render Point
				if(element is Visualization){
					(element as Visualization).renderThubmnail();
				}
				else{
					element.graphics.beginFill(point.color);
					element.graphics.drawCircle(element.width/2,element.height/2,element.width/2);
				}
				
				if(this.toolTip != null){
					element.toolTip = point.text;
				}
			}
		}
		
		override public function getLayout(source:Visualization):Array{
			var layout:Array = new Array;
			var xMin:Number = source.getValue(this.xMin) as Number;
			var xMax:Number = source.getValue(this.xMax) as Number;
			var pixelsPerX:Number = source.width/(xMax - xMin);
			var pixelOffset:Number = pixelsPerX * xMin;
			for each(var point:IDataPoint in source.dataPoints){
				var size:Number = source.getValue(this.elementSize, point) as Number;
				var axisValue:Number = source.getValue(this.xAxis, point) as Number;
				layout.push({
					item:point,
					x:((axisValue * pixelsPerX) - (size/2) - pixelOffset),
					y:((source.height/2) - (size/2)),
					width:size,
					height:size,
					color:(source.getValue(this.elementColor, point) as Number),
					text:(source.getValue(this.toolTip, point) as String)
					});
			}
			return layout;
		}
		
		override public function clone():VisualizationRenderer{
			var newVis:OneDimensional = new OneDimensional();
			this.initClone(newVis);
			return newVis;
		}
		override protected function initClone(rend:VisualizationRenderer):void{
			var newVis:OneDimensional = rend as OneDimensional;
			newVis.elementColor = this.elementColor;
			newVis.elementSize = this.elementSize;
			newVis.lineColor = this.lineColor;
			newVis.xAxis = this.xAxis;
			super.initClone(rend);
		}
		
	}
}